jQuery(function() {
	// 新建投标
	jQuery('.addbid').on('click', function() {
		window.location = window.rootPath + "/ui/bid/bidadd";
	});

	// /查看投标
	jQuery('.seeBtn').on('click', function() {
		var selects = getSelect("#dataTable", 1);
		if (selects.length > 1) {
			$.springLemonDialog({
				content : '一次只能对查看一条记录',// 确认文本
				clickButton : function(sender, modal, index) {// 按钮事件处理
					$(this).closeDialog(modal);
				}
			});
		} else if (selects.length == 0) {
			$.springLemonDialog({
				content : '请选择要发起查看的记录',// 确认文本
				clickButton : function(sender, modal, index) {// 按钮事件处理
					$(this).closeDialog(modal);
				}
			});
		} else if (selects.length == 1) {
			var id = selects[0].id;
			var url = window.rootPath + '/ui/bid/bidsee/' +'?id='+ id;
			window.location = url;
		}
	})

	// 提交投标
	jQuery('.auditBtn').on('click',function() {
				var selects = getSelect("#dataTable", 1);
				if (selects.length > 1) {
					$.springLemonDialog({
						content : '一次只能对一条记录发起审批',// 确认文本
						clickButton : function(sender, modal, index) {// 按钮事件处理
							$(this).closeDialog(modal);
						}
					});
				} else if (selects.length == 0) {
					$.springLemonDialog({
						content : '请选择要发起审批的记录',// 确认文本
						clickButton : function(sender, modal, index) {// 按钮事件处理
							$(this).closeDialog(modal);
						}
					});
				} else if (selects.length == 1) {
					if (selects[0].auditState == '通过') {
						alert(selects[0].name + '已经审批通过');
						return;
					}
					if (selects[0].auditState == '已提交'
							|| selects[0].auditState == '审批中') {
						alert(selects[0].name + '已经提交审批');
						return;
					}
					if (!selects[0].quote&&!selects[0].quoteForeign){
						alert(selects[0].name + '未引入报价单，请先引入报价单');
						return;
					}
					var bidId = selects[0].id;
					var url = window.rootPath + '/ui/bid/audit/' + bidId;
					window.location = url;
				}
			});
	// 察看审批
	jQuery('.audit-nodes-btn').on(
			'click',
			function() {
				var selects = getSelect("#dataTable", 1);
				if (selects.length < 1)
					return;
				if (selects[0].auditState == '未提交') {
					alert(selects[0].name + '还没有提交审批');
					return;
				}
				var url = window.rootPath + '/ui/task/audit-nodes/bid/'
						+ selects[0].id;
				jQuery.get(encodeURI(url), null, function(html) {
					jQuery('#audit-nodes-modal-content').html(html);
					jQuery('#audit-nodes-modal').modal("show");
				});
			});
	//获取选中状态
	 $(function(){
		  $(":radio").click(function(){
			  serverData.key = '';
			  var auditState=$('input:radio[name="isReceived"]:checked').val();
			  serverData.auditState = auditState;
			  serverTable.draw();
		  });
	});
	// ///////获得投标的列表
	var auditState =  $('input:radio[name="isReceived"]:checked').val();
	var serverData = {};
	serverData.key = '';
	serverData.auditState = auditState;
	var serverTable;
	jQuery(document).ready(function() {
		serverTable = jQuery('#dataTable')
								.DataTable(
										{
											"serverSide" : true,
											"ordering" : false,
											"ajax" : {
												"dataType" : "json",
												"type" : "get",
												"cache" : false,
												"url" : window.rootPath
														+ '/ui/bid/getpage',
												"data" : function(d) {
													return $.extend({}, d,
															serverData);
												}
											},
											columns : [
													{
														data : null
													},
													{
														data : null,
														defaultContent : "",
														render : function(data,
																type, full,
																meta) {
															var id = data.id
															var operate = '<input type="checkbox" class="checkchild">';
															return operate;
														}
													},
													{
														data : 'name',
										                render:function(data,type,full,meta){
										                	if(data == null || data == undefined || data == ""){
																return data;
															}else{
																return (data.length > 18) ? '<font title="'+data+'">'+data.substring(0,18)+'...'+'</font>' : data;
															}
														}
													},
													{
														data : 'type'
													},
													{
														data : 'cash'
													},
													{
														data : 'projectType'
													},
													{
														data : 'launcher'
													},
													{
														data : 'launch'
													},
													{
														data : 'ownerUnit',
										                render:function(data,type,full,meta){
										                	if(data == null || data == undefined || data == ""){
																return data;
															}else{
																return (data.length > 18) ? '<font title="'+data+'">'+data.substring(0,18)+'...'+'</font>' : data;
															}
														}
													},
													{
														data : 'bidUnit',
										                render:function(data,type,full,meta){
										                	if(data == null || data == undefined || data == ""){
																return data;
															}else{
																return (data.length > 16) ? '<font title="'+data+'">'+data.substring(0,16)+'...'+'</font>' : data;
															}
														}
													},
													{
														data : 'auditState',
														render : function(data,
																type, full,
																meta) {
															var dOut;
															if (data == '审批中') {
																dOut = '<td class="center" ><span class="badge label-pill label-primary">审批中</span></td>';
															} else if (data == '驳回') {
																dOut = '<td class="center" ><span class="badge label-pill label-danger">驳回</span></td>';
															} else if (data == '通过') {
																dOut = '<td class="center" ><span class="badge label-pill label-success">通过</span></td>';
															} else if (data == '未提交') {
																dOut = '<td class="center" ><span class="badge label-pill label-default">未提交</span></td>';
															} else if (data == '已提交') {
																dOut = '<td class="center" ><span class="badge label-pill label-warning">已提交</span></td>';
															}
															return dOut;
														}
													} 
												],
											"drawCallback" : function(oSettings) {
												var k = 0;
												var total = oSettings.aiDisplay.length
														+ oSettings._iDisplayStart;
												for (var i = oSettings._iDisplayStart, iLen = total; i < iLen; i++) {
													$(
															'td:eq(0)',
															oSettings.aoData[oSettings.aiDisplay[k]].nTr)
															.text(i + 1);
													k++;
												}
											}
										});
					});

	// //删除投标
	jQuery('.delBtn').on(
			'click',
			function() {
				var selects = getSelect("#dataTable", 1);
				var flag = 0;
				for (var i = 0; i < selects.length; i++) {
					var auditState = selects[i].auditState;
					if (auditState == '已提交' || auditState == '审批中'
							|| auditState == '通过') {
						$.springLemonDialog({
							content : '有审批中或审批已通过的记录，不能删除',// 确认文本
							clickButton : function(sender, modal, index) {// 按钮事件处理
								$(this).closeDialog(modal);
							}
						});
						flag = 1;
						return;
					}
				}
				var url = window.rootPath + "/ui/bid/delBid";
				delSelect(selects, url, serverTable)
			});

	// ///编辑投标反馈
	jQuery('.backBtn').on(
			'click',
			function() {
				var selects = getSelect("#dataTable", 1);
				if (selects.length > 1) {
					$.springLemonDialog({
						content : '只能编辑一条投标的反馈',// 确认文本
						clickButton : function(sender, modal, index) {// 按钮事件处理
							$(this).closeDialog(modal);
						}
					});
				} else if (selects.length == 0) {
					$.springLemonDialog({
						content : '请选择要编辑的记录',// 确认文本
						clickButton : function(sender, modal, index) {// 按钮事件处理
							$(this).closeDialog(modal);
						}
					});
				} else if (selects.length == 1) {
					var auditState = selects[0].auditState;
					if (auditState == '通过' ) {
						var id = selects[0].id;
						window.location = window.rootPath + "/ui/bid/backbid"
								+ "?id=" + id;
						
					} else {
						$.springLemonDialog({
							content : '审批中，不能编辑',// 确认文本
							clickButton : function(sender, modal, index) {// 按钮事件处理
								$(this).closeDialog(modal);
							}
						});
					}

				}

			});

	// ////投标编辑
	jQuery('.editBtn').on(
			'click',
			function() {
				var selects = getSelect("#dataTable", 1);
				if (selects.length > 1) {
					$.springLemonDialog({
						content : '只能编辑一条记录',// 确认文本
						clickButton : function(sender, modal, index) {// 按钮事件处理
							$(this).closeDialog(modal);
						}
					});
				} else if (selects.length == 0) {
					$.springLemonDialog({
						content : '请选择要编辑的记录',// 确认文本
						clickButton : function(sender, modal, index) {// 按钮事件处理
							$(this).closeDialog(modal);
						}
					});
				} else if (selects.length == 1) {
					var auditState = selects[0].auditState;
					if (auditState == '审批中' || auditState == '已提交') {
						$.springLemonDialog({
							content : '审批中，不能编辑',// 确认文本
							clickButton : function(sender, modal, index) {// 按钮事件处理
								$(this).closeDialog(modal);
							}
						});
					} else if (auditState == '通过') {
						$.springLemonDialog({
							content : '已经通过审批，不能编辑',// 确认文本
							clickButton : function(sender, modal, index) {// 按钮事件处理
								$(this).closeDialog(modal);
							}
						});
					} else {
						var id = selects[0].id;
						window.location = window.rootPath + "/ui/bid/bidedit"
								+ "?id=" + id;
					}

				}

			});
	// /////列表中的全选
	$("#checkAll").click(function() {
		var check = $(this).prop("checked");
		$(".checkchild").prop("checked", check);
	});
	//投标查询
	jQuery('.searchBtn').on("click",function(){
	   var key=jQuery('#search').val();
	   serverData.key = key;
	   serverTable.draw();
	})
	//撤销查询
	jQuery('.resetBtn').on("click",function(){
	   serverData.key = '';
	   serverTable.draw();
	   jQuery('#search').val('');
	})

})
